package programmercarl.动态规划.C14;

import java.util.Arrays;

class Solution {
    public int lastStoneWeightII(int[] stones) {
        //尝试放最多的
        int sum = Arrays.stream(stones).sum();
        int target = sum / 2;
        int[] dp = new int[target + 1];//这里恰好是1/2向下取整能放的重量
        for (int i = 0; i < stones.length; i++) {
            for (int j = target; j >=stones[i]; j--) {
                dp[j] = Math.max(dp[j], dp[j - stones[i]] + stones[i]);
            }
        }
        return sum - 2*dp[target];
    }
}